/*
 * Copyright 2012 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.pc.domain.impl;

import java.io.Serializable;
import org.opu.pc.domain.SystemSettingsManagement;
import org.opu.pc.domain.dao.SystemSettingsDao;
import org.opu.pc.domain.domains.SystemSettings;

/**
 *
 * @author yura
 */
public class SystemSettingsManagementImpl implements SystemSettingsManagement{
    
    private SystemSettingsDao systemSettingsDao;

    public SystemSettingsManagementImpl(SystemSettingsDao systemSettingsDao) {
        this.systemSettingsDao = systemSettingsDao;
    }
    
    @Override
    public SystemSettings get(Option option) {
        return systemSettingsDao.get(option.name());
    }

    @Override
    public boolean getBoolean(Option option) {
        SystemSettings get = get(option);
        if (get != null) {
            checkCompatibilityOfValue(Boolean.class, get);
            return Boolean.parseBoolean(get.getValue());
        } else {
            return false;
        }
    }

    @Override
    public int getInteger(Option option) {
        SystemSettings get = get(option);
        if (get != null) {
            checkCompatibilityOfValue(Integer.class, get);
            try {
                return Integer.parseInt(get.getValue());
            } catch (Exception ex) {
                return -1;
            }
        } else {
            return -1;
        }
    }

    @Override
    public DefaultAuthenticationOption getDefaultAuthenticationOption(Option option) {
        SystemSettings get = get(option);
        if (get != null) {
            checkCompatibilityOfValue(DefaultAuthenticationOption.class, get);
            return Enum.valueOf(DefaultAuthenticationOption.class, get.getValue());
        } else {
            return DefaultAuthenticationOption.NoCredentials;
        }
    }
    
    @Override
    public void ensureDataRegistered() {
        
    }
    
    @Override
    public void setValue(Option option, Serializable value) {
        checkCompatibility(option.getOprionType(), value.getClass());
        systemSettingsDao.saveOrUpdate(new SystemSettings(option.name(), String.valueOf(value)));
    }

    private void checkCompatibilityOfValue(Class<?> clazz, SystemSettings get) 
            throws IllegalArgumentException {
        checkCompatibility(clazz, Enum.valueOf(Option.class, get.getId()).getOprionType());
    }

    private void checkCompatibility(Class<?> option, Class<?> value) 
            throws IllegalArgumentException {
        if (option != value){
            throw new IllegalArgumentException("Incompatible calsses: Expect: " 
                    + option + "; get: " + value);
        }
    }
    
}
